package com.xy6.jvm.thread.stopthread;

import java.util.concurrent.TimeUnit;

/**
 * 线程操作示例。
 * 
 * @author zhang
 * @since 2018-05-03
 */
public class StopThread2 {

	private static boolean stopRequested;
	
	private static synchronized void requestStop(){
		stopRequested = true;
	}
	
	private static synchronized boolean stopRequest(){
		return stopRequested;
	}
	
	public static void main(String[] args) throws InterruptedException{
		Thread backgroundThread = new Thread(new Runnable(){
			@SuppressWarnings("unused")
			@Override
			public void run() {
				int i=0;
				while(!stopRequest()){
					i++;
				}
			}
		});
		backgroundThread.start();
		TimeUnit.SECONDS.sleep(1);
		requestStop();
	}
	
}
